package com.tpp.base.controller;

import com.tpp.base.service.AdminItemService;
import com.tpp.base.vo.ItemSearchVo;
import com.tpp.comm.pojo.TpItem;
import com.tpp.comm.vo.ItemSchedulerVo;
import com.tpp.comm.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/s/item")
public class AdminItemController {

    @Resource
    private AdminItemService adminItemService;


    @PostMapping("/findItem")
    public Result findItem(@RequestBody ItemSearchVo itemSearchVo){
        return Result.success(adminItemService.findItems(itemSearchVo));
    }

    @PostMapping("/findItemGroupByStartTime")
    public Result findItemGroupByStartTime(@RequestBody ItemSearchVo itemSearchVo){
        return Result.success(adminItemService.findItemGroupByStartTime(itemSearchVo));
    }

    @PostMapping("/add")
    public Result addItem(@RequestBody TpItem tpItem){
        try {
            adminItemService.addItem(tpItem);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/update")
    public Result updateItem(@RequestBody TpItem tpItem){
        try {
            adminItemService.updateItem(tpItem);
            return Result.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("修改失败");
        }
    }

    @GetMapping("/delete")
    public Result deleteItem(Integer id){
        try {
            adminItemService.deleteItem(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("删除失败");
        }
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        return Result.success(adminItemService.findById(id));
    }

    @PostMapping("/addItemScheduler")
    public Result addItemScheduler(@RequestBody ItemSchedulerVo itemSchedulerVo){
        try {
            adminItemService.addItemScheduler(itemSchedulerVo);
            return Result.success("添加排期成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success("添加排期失败");
        }

    }
}
